package com.perseygroup.apps.tools.stages;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.perseygroup.apps.tools.ws.WsStagesOperations;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

public class BaseFeedParser {

	//static String feedUrlString = "http://rams.dyndns-home.com/resourcespersey/fluxstages/listestages.xml";
	static String feedUrlString = "http://10.0.2.2/resourcespersey/fluxstages/listestages.xml";
	
	// names of the XML tags
	static final String LISTE = "liste";
	static final String STAGE = "stage";
	static final String TITRE = "titre";
	static final String ENTREPRISE = "entreprise";
	static final String LIEU = "lieu";
	static final String DATEPUB = "date";
	static final String DESCRIPTION = "description";
	private int start = 0;//debut de la liste
	private int end = 0;//fin de la liste
	
	private final URL feedUrl;

	protected BaseFeedParser(){
		try {
			this.feedUrl = new URL(feedUrlString);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}
	
	//protected BaseFeedParser(int start, int end){
	public BaseFeedParser(int start, int end){
		try {
			this.start = start;
			this.end = end;
			
			this.feedUrl = new URL(feedUrlString);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	protected InputStream getInputStream() {
		try {
			String listeRecup = WsStagesOperations.getListeStages(this.start, this.end);
			
			InputStream is = new ByteArrayInputStream(listeRecup.getBytes("UTF-8"));
			
			//return feedUrl.openConnection().getInputStream();
			return is;
			
			
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<Stage> parse() {
		final Stage currentMessage = new Stage();
		RootElement root = new RootElement(LISTE);
		final List<Stage> stages = new ArrayList<Stage>();
		//Element itemlist = root.getChild(STAGE);
		Element item = root.getChild(STAGE);
		item.setEndElementListener(new EndElementListener(){
			public void end() {
				stages.add(currentMessage.copy());
			}
		});
		item.getChild(TITRE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTitre(body);
			}
		});
		item.getChild(ENTREPRISE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setNomEntreprise(body);
			}
		});
		item.getChild(LIEU).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setLieu(body);
			}
		});
		item.getChild(DATEPUB).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setDatePub(body);
			}
		});
		item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setDescription(body);
			}
		});
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return stages;
	}
}